/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wwl.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.function.Consumer;

import static org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition;

/**
 * @author wangweili
 * @version 1.0
 * @date 2021/3/23 9:32 下午
 */
@Slf4j
@Component
public class RabbitMqRegister {

    private ConfigurableApplicationContext context;

    private final ApplicationContext applicationContext;

    public RabbitMqRegister(ConfigurableApplicationContext context, ApplicationContext applicationContext) {
        this.context = context;
        this.applicationContext = applicationContext;
    }

    public <T> T getBean(String beanName, Class<T> t) {
        return context.getBean(beanName, t);
    }

    public boolean containsBean(String beanName) {
        return context.containsBean(beanName);
    }

    public <T> boolean registerBean(String beanName, T bean) {
        context = (ConfigurableApplicationContext) applicationContext;
        context.getBeanFactory().registerSingleton(beanName, bean);
        return true;
    }

    /**
     * 命名Bean的注册方式
     *
     * @param beanName  命名
     * @param beanClass beanClass
     * @param consumer 自定义属性
     */
    private void registerBeanDefinition(String beanName, Class<?> beanClass, Consumer<BeanDefinitionBuilder> consumer) {
        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) context;
        BeanDefinitionBuilder beanDefinitionBuilder = genericBeanDefinition(beanClass);
        //自定义addProperty
        consumer.accept(beanDefinitionBuilder);
        if (StringUtils.hasText(beanName)) {
            registry.registerBeanDefinition(beanName, beanDefinitionBuilder.getBeanDefinition());
            return;
        }
        BeanDefinitionReaderUtils.registerWithGeneratedName(beanDefinitionBuilder.getBeanDefinition(), registry);
    }

    /**
     * 非命名bean的注册方式
     *
     * @param beanClass beanClass
     * @param consumer 自定义属性
     */
    private void registerBeanDefinition(Class<?> beanClass, Consumer<BeanDefinitionBuilder> consumer) {
        registerBeanDefinition(null, beanClass, consumer);
    }

}